﻿using BroadGoalComponents;
using BroadGoalComponents.Dialogs;
using Custom.Framework;
using Custom.Model.Dto;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Main
{
    public partial class frmLogin : PlatFormBase
    {
        private SysUser user = null;
        private SysUserRepository userDAL = new SysUserRepository();

        public frmLogin()
        {
            InitializeComponent();

            DragWindow drag = new DragWindow();
            drag.BindEvent(this, this.panel1, this.panel2);

            this.txtPassword.InnerTextBox.PasswordChar = '*';
            this.bgButtonFirst1.Focus();
            this.bgButtonFirst1.Select();

            this.txtUsername.Text = "admin";
        }

        private void bgMinimizeButton1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void bgCloseButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.user = this.userDAL.Get(w => w.username == this.txtUsername.Text)?.FirstOrDefault();
            if (user != null && user.autoremeber == "1")
            {
                this.txtPassword.Text = user.password;
                this.ckRemeber.Checked = true;
            }
        }

        private void bgButtonFirst1_Click(object sender, EventArgs e)
        {
            this.user = this.userDAL.Get(w => w.username == this.txtUsername.Text && w.password == this.txtPassword.Text)?.FirstOrDefault();
            if (user == null)
            {
                WarningDialog.Show("用户名或密码错误");
                return;
            }
            else
            {
                this.user.autoremeber = this.ckRemeber.Checked ? "1" : "0";
                this.userDAL.Update(user);
                GlobalParam.User = this.user;

                this.Hide();
                frmMain frm = new frmMain();
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    this.Close();
                }
            }
        }
    }
}
